package com.ep.thirdPlatformProject.oa.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ep.thirdPlatformProject.oa.entity.OACompanyEntity;
import com.ep.thirdPlatformProject.oa.entity.OAHrmResourceEntity;
import com.ep.thirdPlatformProject.oa.entity.vo.OACompanyVO;
import com.ep.thirdPlatformProject.oa.entity.vo.OAOrgChangeVO;
import com.ep.thirdPlatformProject.oa.mapper.OACompanyMapper;
import com.ep.thirdPlatformProject.oa.service.OACompanyService;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.BoundListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
@DS("oa_db")
@Slf4j
@RequiredArgsConstructor
public class OACompanyServiceImpl extends ServiceImpl<OACompanyMapper, OACompanyEntity> implements OACompanyService {

    private final OACompanyMapper oaCompanyMapper;

    private final RedisTemplate<String, OAOrgChangeVO> redisTemplate;

    /**
     * OA公司名单
     */
    private final static String OA_COMPANY_LIST = "OA_COMPANY_LIST";

    /**
     * 获取变更公司列表
     *
     * @return {@link List }<{@link OAOrgChangeVO }>
     */
    @Override
    public List<OAOrgChangeVO> getChangeCompanyList() {

        // 获取当天的开始时间
        DateTime start = DateUtil.beginOfDay(DateUtil.date());
        Boolean hasKey = redisTemplate.hasKey(OA_COMPANY_LIST);
        BoundListOperations<String, OAOrgChangeVO> listOps = redisTemplate.boundListOps(OA_COMPANY_LIST);
        if (Boolean.FALSE.equals(hasKey)) {
            MPJLambdaWrapper<OACompanyEntity> queryWrapper = new MPJLambdaWrapper<OACompanyEntity>()
                    .select(OACompanyEntity::getId)
                    .selectAs(OACompanyEntity::getCompanyName, OAOrgChangeVO::getOrgName)
                    .selectAs(OACompanyEntity::getCompanyCode, OAOrgChangeVO::getOrgCode)
                    .selectAs(OACompanyEntity::getLevel, OAOrgChangeVO::getLevel)
                    .selectAs(OACompanyEntity::getCreateTime, OAOrgChangeVO::getCreateTime)
                    .selectAs(OACompanyEntity::getModifiedTime, OAOrgChangeVO::getModifiedTime)
                    .selectAs("c", OAHrmResourceEntity::getLastname, OAOrgChangeVO::getCreator)
                    .selectAs("b", OAHrmResourceEntity::getLastname, OAOrgChangeVO::getModifier)
                    .selectAs("b", OAHrmResourceEntity::getLastname, OAOrgChangeVO::getModifyUsername)
                    .selectAs("b", OAHrmResourceEntity::getWorkCode, OAOrgChangeVO::getModifyUserNo)
                    .leftJoin(OAHrmResourceEntity.class, "b", OAHrmResourceEntity::getId, OACompanyEntity::getModifier)
                    .leftJoin(OAHrmResourceEntity.class, "c", OAHrmResourceEntity::getId, OACompanyEntity::getCreator)
                    .ge(OACompanyEntity::getModifiedTime, start);
            List<OAOrgChangeVO> vos = oaCompanyMapper.selectJoinList(OAOrgChangeVO.class, queryWrapper);
            if (!vos.isEmpty()) {
                OAOrgChangeVO[] values = vos.toArray(new OAOrgChangeVO[]{});
                listOps.leftPushAll(values);
                listOps.expire(12, TimeUnit.HOURS);
            }
            return vos;
        }
        return listOps.range(0, -1);
    }

    /**
     * 通过映射公司编码获得公司id
     *
     * @param code 代码
     * @return {@link String }
     */
    @Override
    public String getOACompanyByReflectCode(String code) {
        OACompanyVO company = oaCompanyMapper.getOACompanyByReflectCode(code);
        if (company == null || company.getId() == null) {
            throw new RuntimeException(code + "当前公司code在oa不存在！");
        }
        return String.valueOf(company.getId());
    }


}
